package exp.parser;

import grammar.parser.FuncFParams;
import word.parser.WordElement;

import java.util.ArrayList;
import java.util.List;

public class LOrExp extends ExpParser {
    private final ExpParser left;
    private final ExpParser right;

    public LOrExp(ExpParser left, ExpParser right) {
        super(false, left.isConst() && (right == null || right.isConst()));
        this.left = left;
        this.right = right;
    }

    public static LOrExp parseLOrExp() {
        List<ExpParser> expParserList = new ArrayList<>();
        ExpParser left = LAndExp.parseLAndExp();
        expParserList.add(left);
        while (getAnalyzingWord().getType() == WordElement.WordType.OR) {
            analyzeNextWord();
            expParserList.add(LAndExp.parseLAndExp());
        }
        for (int i = 1; i <= expParserList.size(); i++) {
            ExpParser right = i < expParserList.size() ? expParserList.get(i) : null;
            left = new LOrExp(left, right);
        }
        return (LOrExp) left;
    }

    @Override
    public LVal toLVal() {
        return null;
    }

    @Override
    public Integer compute() {
        int l = left.getValue();
        if (right == null) {
            return l;
        }
        if (l != 0) {
            return l;
        }
        if (right.getValue() != 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public FuncFParams.FuncFParam checkDimension() {
        return new FuncFParams.FuncFParam();
    }
}
